<script setup>
    import { ref, onMounted } from 'vue';
    //引入echarts
    import * as echarts from 'echarts';
    let charts = ref();
    //组件挂载完毕初始化图形图标
    onMounted(() => {
        let mychart = echarts.init(charts.value);
        //设置配置项
        let option = {
            tooltip: {
                trigger: 'item',
                formatter: '{a} <br/>{b}: {c} ({d}%)'
            },
            legend: {
                right: 30,
                top: 50,
                orient: 'vertical',//图例组件方向的设置
                textStyle: {
                    color: 'white',
                    fontSize: 14
                },
                formatter: function(name) {
                    // 查找对应的数据项
                    let data = option.series[0].data;
                    let total = 0;
                    let value = 0;
                    for (let i = 0; i < data.length; i++) {
                        total += data[i].value;
                        if (data[i].name === name) {
                            value = data[i].value;
                        }
                    }
                    let percent = ((value / total) * 100).toFixed(1);
                    return name + ' ' + percent + '%';
                }

            },
            series: [
                {
                    name: '年龄比例',
                    type: 'pie',
                    radius: ['40%', '70%'],
                    center: ['35%', '50%'], // 调整饼图位置，第一个值越小越靠左
                    avoidLabelOverlap: false,
                    itemStyle: {
                        borderRadius: 10,
                        borderColor: '#fff',
                        borderWidth: 2
                    },
                    label: {
                        show: false,
                        position: 'inside',
                        color: 'white',
                        formatter: '{d}%'
                    },

                    labelLine: {
                        show: false
                    },
                    data: [
                        { value: 348, name: '10岁以下' },
                        { value: 735, name: '10-18岁' },
                        { value: 1580, name: '18-30岁' },
                        { value: 684, name: '30-40岁' },
                        { value: 300, name: '40-60岁' },
                        { value: 200, name: '60岁以上' }
                    ]
                }
            ],
            //调整图形图标的位置
            grid: {
                left: 0,
                top: 0,
                right: 0,
                bottom: 0
            }
        };
        mychart.setOption(option);
    });
</script>
<template>
    <div class="box">
        <div class="top">
            <div>
                <p class="title">年龄比例</p>
                <img
                    src="../../images/dataScreen-title.png"
                    alt=""
                >
            </div>
        </div>
        <!-- 图形图标的容器 -->
        <div
            class="charts"
            ref="charts"
        ></div>
    </div>
</template>
<style scoped>
    .box {
        background: url(../../images/dataScreen-main-lb.png) no-repeat;
        background-size: 100% 100%;
        margin-top: 10px;
    }

    .top {
        margin-left: 20px;
        height: 70px;
        display: flex;
        align-items: center;
    }

    .top .title {
        color: white;
        font-size: 20px;
    }

    .top img {
        margin-top: 10px;
    }

    .charts {
        height: 260px;
    }
</style>